<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Begin Jekyll SEO tag v2.7.1 -->
<title>Bitmap | 微光</title>
<meta name="generator" content="Jekyll v4.2.1">
<meta property="og:title" content="Bitmap">
<meta property="og:locale" content="en_US">
<meta name="description" content="Bitmap">
<meta property="og:description" content="Bitmap">
<link rel="canonical" href="/redis/data-type/bitmap.html">
<meta property="og:url" content="/redis/data-type/bitmap.html">
<meta property="og:site_name" content="微光">
<meta property="og:type" content="article">
<meta property="article:published_time" content="2022-01-18T01:52:04+08:00">
<meta name="twitter:card" content="summary">
<meta property="twitter:title" content="Bitmap">
<script type="application/ld+json">
{"description":"Bitmap","url":"/redis/data-type/bitmap.html","headline":"Bitmap","dateModified":"2022-01-18T01:52:04+08:00","datePublished":"2022-01-18T01:52:04+08:00","@type":"BlogPosting","mainEntityOfPage":{"@type":"WebPage","@id":"/redis/data-type/bitmap.html"},"@context":"https://schema.org"}</script>
<!-- End Jekyll SEO tag -->
<link rel="stylesheet" href="/assets/css/normalize.css">
  <link rel="stylesheet" href="/assets/css/style.css">
<link type="application/atom+xml" rel="alternate" href="/feed.xml" title="微光">
<script>MathJax={"tex":{"inlineMath":[["$","$"],["\\(","\\)"]],"displayMath":[["$$","$$"],["\\[","\\]"]]},"svg":{"fontCache":"global"}}</script><script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script><script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
</head>
<body>
<header class="site-header">

  <div class="wrapper">
<a class="site-title" rel="author" href="/">微光</a><nav class="site-nav">
        <input type="checkbox" id="nav-trigger" class="nav-trigger">
        <label for="nav-trigger">
          <span class="menu-icon">
            <svg viewbox="0 0 18 15" width="18px" height="15px">
              <path d="M18,1.484c0,0.82-0.665,1.484-1.484,1.484H1.484C0.665,2.969,0,2.304,0,1.484l0,0C0,0.665,0.665,0,1.484,0 h15.032C17.335,0,18,0.665,18,1.484L18,1.484z M18,7.516C18,8.335,17.335,9,16.516,9H1.484C0.665,9,0,8.335,0,7.516l0,0 c0-0.82,0.665-1.484,1.484-1.484h15.032C17.335,6.031,18,6.696,18,7.516L18,7.516z M18,13.516C18,14.335,17.335,15,16.516,15H1.484 C0.665,15,0,14.335,0,13.516l0,0c0-0.82,0.665-1.483,1.484-1.483h15.032C17.335,12.031,18,12.695,18,13.516L18,13.516z"></path>
            </svg>
          </span>
        </label>

        <div class="trigger">
<a class="page-link" href="/about/">About</a><a class="page-link" href="/doc/main">DocMain</a><a class="page-link" href="/sites.html">Sites</a>
</div>
      </nav>
</div>
</header>
<div class="x-collections-bar">
  <a href="/">Home</a>
  
    
      <a href="/algri/index">算法</a>
    
  
    
      <a href="/java/index">Java</a>
    
  
    
  
    
  
    
      <a href="/redis/index">Redis</a>
    
  
</div>

<div class="x-content-side-layout x-container">
  <div class="x-left">
    <h3>本文目录</h3>
    <ul id="toc" class="section-nav">
<li class="toc-entry toc-h2"><a href="#bitmap">Bitmap</a></li>
<li class="toc-entry toc-h2"><a href="#%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4">相关命令</a></li>
</ul>
  </div>
  <div class="x-content">
    <h1><strong>Redis - Bitmap</strong></h1>
    <small>2022-01-18 01:52:04 +0800</small>
    
    <article class="x-article"><h2 id="bitmap">
<a class="anchor" href="#bitmap" aria-hidden="true"><span class="octicon octicon-link"></span></a>Bitmap</h2>

<p>Bitmap 不是一个实际上的数据类型，而是一组定义在字符串上的面向bit的操作集合。因为字符串是二进制安全的 blob，最大长度是 512MB，所以它可以设置 $2^{32}$ 个不同的 bit。</p>

<p>Bit 操作被分成两组：常数时间的单 bit 操作，类似于把一个 bit 设置为 0 或 1，或者获取它的值；和对一组 bit 进行的操作，例如统计指定范围内的bit数量。</p>

<p>Bitmap 的一个最大优点是它在存储信息时极为节省空间。比如一个系统中，不同的用户用递增的用户 ID，那么用 bitmap 只需要 512MB 内存就可以记录 40一个用户的单 bit 信息。</p>

<p>设置和获取 bit 使用命令 <code class="language-plaintext highlighter-rouge">SETBIT</code> 和 <code class="language-plaintext highlighter-rouge">GETBIT</code>：</p>

<div class="language-console highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="gp">&gt;</span><span class="w"> </span>setbit key 10 1
<span class="go">(integer) 1
</span><span class="gp">&gt;</span><span class="w"> </span>getbit key 10
<span class="go">(integer) 1
</span><span class="gp">&gt;</span><span class="w"> </span>getbit key 11
<span class="go">(integer) 0
</span></code></pre></div></div>

<p>命令 <code class="language-plaintext highlighter-rouge">SETBIT</code> 把第一个参数作为 bit 的位置，第二个参数作为 bit 的值（取 0 或 1）。命令自动扩展字符串的长度，在设置的 bit 为超出字符串长度时。</p>

<p>命令 <code class="language-plaintext highlighter-rouge">GETBIT</code> 只是返回指定索引处的 bit 值。超出字符串长度范围之外的 bit 总是被认为是 0.</p>

<p>有三个命令来操作一组 bit：</p>

<ol>
  <li>
<code class="language-plaintext highlighter-rouge">BITOP</code> 在不同字符串之间进行按位操作。提供的操作有 AND、OR、XOR 和 NOT。</li>
  <li>
<code class="language-plaintext highlighter-rouge">BITCOUNT</code> 进行统计，返回所有为 1 的 bit 的数量。</li>
  <li>
<code class="language-plaintext highlighter-rouge">BITPOS</code> 返回第一个具有指定值的 bit 的位置。</li>
</ol>

<p><code class="language-plaintext highlighter-rouge">BITPOS</code> 和 <code class="language-plaintext highlighter-rouge">BITCOUNT</code> 都可以按字节操作字符串，而非遍历整个字符串。下面是一个使用 <code class="language-plaintext highlighter-rouge">BITCOUNT</code> 命令的简单例子：</p>

<div class="language-console highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="gp">&gt;</span><span class="w"> </span>setbit key 0 1
<span class="go">(integer) 0
</span><span class="gp">&gt;</span><span class="w"> </span>setbit key 100 1
<span class="go">(integer) 0
</span><span class="gp">&gt;</span><span class="w"> </span>bitcount key
<span class="go">(integer) 2
</span></code></pre></div></div>

<p>常见的使用场景有：</p>

<ul>
  <li>各种实时分析</li>
  <li>存储空间高线且高性能的布尔信息，和一个对象的 ID 关联。</li>
</ul>

<p>把多个 Bitmap 拆分成多个 key 很简单，比如，因为要对数据集进行分片，或者因为通常建议避免使用巨型 key。</p>

<h2 id="相关命令">
<a class="anchor" href="#%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4" aria-hidden="true"><span class="octicon octicon-link"></span></a>相关命令</h2>

<ul>
  <li>
<code class="language-plaintext highlighter-rouge">BITCOUNT key [start end [BYTE|BIT]]</code><br>
  <small>Count set bits in a string</small>
</li>
  <li>
<code class="language-plaintext highlighter-rouge">BITFIELD key [GET encoding offset] [SET encoding offset value] [INCRBY encoding offset increment] [OVERFLOW WRAP|SAT|FAIL]</code><br>
  <small>Perform arbitrary bitfield integer operations on strings</small>
</li>
  <li>
<code class="language-plaintext highlighter-rouge">BITFIELD_RO key GET encoding offset</code><br>
  <small>Perform arbitrary bitfield integer operations on strings. Read-only variant of BITFIELD</small>
</li>
  <li>
<code class="language-plaintext highlighter-rouge">BITOP operation destkey key [key ...]</code><br>
  <small>Perform bitwise operations between strings</small>
</li>
  <li>
<code class="language-plaintext highlighter-rouge">BITPOS key bit [start [end [BYTE|BIT]]]</code><br>
  <small>Find first bit set or clear in a string</small>
</li>
  <li>
<code class="language-plaintext highlighter-rouge">GETBIT key offset</code><br>
  <small>Returns the bit value at offset in the string value stored at key</small>
</li>
  <li>
<code class="language-plaintext highlighter-rouge">SETBIT key offset value</code><br>
  <small>Sets or clears the bit at offset in the string value stored at key</small>
</li>
</ul>

</article>
  </div>
  <div class="x-right">
    <h3>Redis</h3>
    
      <div class="x-collection-section">
        <h4>开始</h4>
        
        <ol>
          
            
              <li><a href="/redis/index.html">开始</a></li>
            
          
            
              <li><a href="/redis/install.html">安装</a></li>
            
          
            
              <li><a href="/redis/concept.html">相关概念预览</a></li>
            
          
        </ol>
      </div>
    
      <div class="x-collection-section">
        <h4>数据类型</h4>
        
        <ol>
          
            
              <li><a href="/redis/data-type/key-and-string.html">Key 和 String</a></li>
            
          
            
              <li><a href="/redis/data-type/list.html">List</a></li>
            
          
            
              <li><a href="/redis/data-type/hash.html">Hash</a></li>
            
          
            
              <li><a href="/redis/data-type/set.html">Set</a></li>
            
          
            
              <li><a href="/redis/data-type/zset.html">Sorted Set</a></li>
            
          
            
              <li class="active"><a href="/redis/data-type/bitmap.html">Bitmap</a></li>
            
          
            
              <li><a href="/redis/data-type/hyperloglog.html">Hyperloglog</a></li>
            
          
            
              <li><a href="/redis/data-type/geo.html">Geo</a></li>
            
          
        </ol>
      </div>
    
      <div class="x-collection-section">
        <h4>其他特性</h4>
        
        <ol>
          
            
              <li><a href="/redis/feature/pipeline.html">流水线处理</a></li>
            
          
        </ol>
      </div>
    
      <div class="x-collection-section">
        <h4>持久化</h4>
        
        <ol>
          
            
              <li><a href="/redis/persist/durability.html">持久化在说什么</a></li>
            
          
            
              <li><a href="/redis/persist/rdb.html">快照（Snapshotting）</a></li>
            
          
            
              <li><a href="/redis/persist/aof.html">AOF</a></li>
            
          
        </ol>
      </div>
    
      <div class="x-collection-section">
        <h4>运维</h4>
        
        <ol>
          
            
              <li><a href="/redis/ops/persistence.html">持久化方案</a></li>
            
          
        </ol>
      </div>
    
      <div class="x-collection-section">
        <h4>应用场景</h4>
        
        <ol>
          
            
              <li><a href="/redis/usecase/blogs.html">应用场景相关文章</a></li>
            
          
        </ol>
      </div>
    
  </div>
</div>
<footer class="site-footer h-card">
  <data class="u-url" href="/"></data>

  <div class="wrapper">

    <div class="footer-col-wrapper">
      <div class="footer-col">
        <p class="feed-subscribe">
          <a href="/feed.xml">
            <svg class="svg-icon orange">
              <use xlink:href="/assets/social-icons.svg#rss"></use>
            </svg><span>Subscribe</span>
          </a>
        </p>
      </div>
      <div class="footer-col">
        <p>Write an awesome description for your new site here. You can edit this line in _config.yml. It will appear in your document head meta (for Google search results) and in your feed.xml site description.</p>
      </div>
    </div>

    <div class="social-links"></div>

  </div>

</footer>
</body>
</html>
